package com.nightsoul.nutz.test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.dao.impl.NutDao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.alibaba.druid.pool.DruidDataSource;
import com.nightsoul.nutz.test.dao.PersonDao;
import com.nightsoul.nutz.test.pojo.Person;
import com.nightsoul.nutz.test.pojo.User;

@IocBean(name="helloworldModule")
/**
 * 要想使用IOC注入, module本身也要交由IOC容器管理
 * @author zj
 *
 */
@Fail("jsp:/error.jsp")
public class Helloworld {
	@Inject(value="init")
	private User user;
	@Inject
	private DruidDataSource dataSource;
	@Inject
	private PersonDao personDao;
	
	@At("/hello")
	@Ok("json")
	public User hello(HttpServletRequest request, HttpServletResponse response, @Param("name") String name) {
		User user = Mvcs.getIoc().get(User.class, "init");
		System.out.println(user);
		
		return new User(name, "zs");
	}

	
	@At("/login")
	@Ok("jsp:/login.jsp") // 内部跳转到 /user/login.jsp
	public void login(HttpServletRequest request, HttpServletResponse response) {
		System.out.println(user);
		System.out.println(this.personDao);
		System.out.println(dataSource);
		
		NutDao nutDao = new NutDao(this.dataSource);
		System.out.println(nutDao);
		
		request.setAttribute("name", "王五");
	}
	
	@At("/save")
	@Ok("json")
	public Person save() {
		Person person = new Person();
		person.setName("张三");
		person.setAge(56);
		this.personDao.save(person);
		return person;
	}
}
